/*
 * Copyright 2011 Clean GWT
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 */
package com.cleangwt.bootstrap.client.ui;

import com.cleangwt.bootstrap.client.style.Btn;
import com.cleangwt.bootstrap.client.style.Dropdowns;
import com.cleangwt.bootstrap.client.style.Styles;
import com.cleangwt.user.client.Elements;
import com.google.gwt.dom.client.AnchorElement;
import com.google.gwt.dom.client.Document;
import com.google.gwt.dom.client.Element;

/**
 * The builder for building dropdown element.
 * 
 * @author Jess
 * @version 1.0
 * @since 1.0
 */
public class DropdownBuilder {
  Element dropdown;
  Element menu;
  Element toggle;

  /**
   * Creates a button dropdown element that wrapping a &lt;div&gt; element within an button.
   * 
   * @param toggleText the toggle text
   * @return the button dropdown element
   */
  static Element createButtonDropdown(String toggleText) {
    Element dropdown = Document.get().createDivElement();
    Element toggle = Document.get().createPushButtonElement();
    Element caret = Document.get().createSpanElement();
    Element menu = Document.get().createULElement();

    dropdown.setClassName(Btn.BTN_GROUP_STYLE);
    dropdown.appendChild(toggle);
    dropdown.appendChild(menu);

    toggle.addClassName(Dropdowns.TOGGLE_STYLE);
    toggle.addClassName(Btn.BTN_STYLE);
    toggle.addClassName(Btn.DEFAULT_STYLE);
    toggle.setAttribute(DataModel.DATA_TOGGLE, DataModel.TOGGLE_DROPDOWN);
    toggle.setInnerHTML(toggleText);
    toggle.appendChild(caret);

    caret.setClassName(Styles.CARET);
    menu.addClassName(Dropdowns.MENU_STYLE);
    return dropdown;
  }

  /**
   * Creates a navigator dropdown element that wrapping a &lt;li&gt; element within an anchor.
   * 
   * @param toggleText the toggle text
   * @return the navigator dropdown element
   */
  static Element createNavDropdown(String toggleText) {
    Element dropdown = Document.get().createLIElement();
    Element toggle = Document.get().createAnchorElement();
    Element caret = Document.get().createSpanElement();
    Element menu = Document.get().createULElement();

    dropdown.setClassName(Dropdowns.DROPDOWN_STYLE);
    dropdown.appendChild(toggle);
    dropdown.appendChild(menu);

    toggle.addClassName(Dropdowns.TOGGLE_STYLE);
    toggle.setAttribute(DataModel.DATA_TOGGLE, DataModel.TOGGLE_DROPDOWN);
    toggle.setInnerHTML(toggleText);
    toggle.appendChild(caret);
    toggle.setAttribute("href", "#");

    caret.setClassName(Styles.CARET);
    menu.addClassName(Dropdowns.MENU_STYLE);
    return dropdown;
  }

  /**
   * Creates a dropdown builder for the given dropdown element.
   * 
   * @param dropdown the dropdown element
   */
  DropdownBuilder(Element dropdown) {
    this.dropdown = dropdown;
    this.toggle = Elements.getByClass(dropdown, DataModel.DATA_TOGGLE);
    this.menu = Elements.getByClass(dropdown, Dropdowns.MENU_STYLE);
  }

  /**
   * Adds a divider element to the dropdown menu.
   * 
   * @return this
   */
  public DropdownBuilder addDivider() {
    Element li = Document.get().createLIElement();
    li.setClassName(Dropdowns.DIVIDER_STYLE);
    menu.appendChild(li);
    return this;
  }

  /**
   * Adds a header element to the dropdown menu.
   * 
   * @return this
   */
  public DropdownBuilder addHeader(String html) {
    Element li = Document.get().createLIElement();
    li.setInnerHTML(html);
    li.setClassName(Dropdowns.HEADER_STYLE);
    menu.appendChild(li);
    return this;
  }

  /**
   * Adds a link with the given html and href.
   * 
   * @param html the html
   * @param href the href
   * @return this
   */
  public DropdownBuilder addLink(String html, String href) {
    Element li = Document.get().createLIElement();
    AnchorElement anchor = Document.get().createAnchorElement();
    anchor.setInnerHTML(html);
    anchor.setHref(href);
    li.appendChild(anchor);
    menu.appendChild(li);
    return this;
  }

  /**
   * Sets the dropup.
   * 
   * @param dropup true to add dropup style, false to remove it
   * @return this
   */
  public DropdownBuilder setDropup(boolean dropup) {
    if (dropup) {
      dropdown.addClassName(Dropdowns.DROPUP_STYLE);
    } else {
      dropdown.removeClassName(Dropdowns.DROPUP_STYLE);
    }
    return this;
  }

  /**
   * Resets the toggle style with dropdown-toggle style and the given type style.
   * 
   * @param style the base style
   */
  public void setStyleBase(String style) {
    assert style != null : "Style must not be null.";
    toggle.setClassName(Dropdowns.TOGGLE_STYLE + " " + style);
  }
}
